package dashboard.utils;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;

public class VelocityTools {

	/**
	 * 百分比转换
	 */
	public double tranRate(double num) {
		DecimalFormat df = new DecimalFormat("0.00");
		if (num == 0) {
			return 0;
		}
		return Double.valueOf(df.format(num * 100));
	}

	public static <T> List<String> getStringList(String string, List<T> list) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		List<String> result = new ArrayList<String>(list.size());
		for (T obj : list) {
			result.add(getString(obj, string));
		}
		return result;
	}

	private static <T> String getString(T m, String string) {
		try {
			return FieldUtils.readField(m, string, true).toString();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;

	}

	public static <T> Object getValues(String zdName, T vo, Class<T> clazz) throws Exception {
		Method[] mm = clazz.getMethods();
		Map<String, Method> methodMap = new HashMap<String, Method>();
		for (Method m : mm) {
			methodMap.put(m.getName(), m);
		}
		String methodName = "get" + zdName.substring(0, 1).toUpperCase() + zdName.substring(1);
		Object value = methodMap.get(methodName).invoke(vo);
		return value;
	}

	/**
	 * 百分比转换 通用报表
	 */
	public static String comRate(String num) {
		if ("--".equals(num)) {
			return "--";
		}
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(Double.parseDouble(num) * 100) + "%";
	}

	// 四位小数，不以千分位分隔，百分比用
	public static String transTo4Point(double num) {
		DecimalFormat df = new DecimalFormat("0.0000");
		if (num == 0) {
			return "0";
		}
		return String.valueOf(Double.valueOf(df.format(num)));
	}

	// 计算占比 num1/num2
	public static String compRateDirect(Object num1, Object num2) {
		if (num1 == null || num2 == null) {
			return "--";
		}
		DecimalFormat df = new DecimalFormat("0.00");
		String strVal1 = String.valueOf(num1);
		String strVal2 = String.valueOf(num2);
		double num_one = Double.valueOf(strVal1);
		double num_two = Double.valueOf(strVal2);

		if (num_two == 0) {
			return "0";
		}
		double sss = (num_one) / num_two;
		if (sss > 0) {
			return df.format(sss * 100) + "%";
		} else {
			return "--";
		}

	}

	public static double compDecimalRate(Object num1, Object num2) {
		if (num1 == null && num2 == null) {
			return 0;
		} else if (num1 == null) {
			return -1;
		} else if (num2 == null) {
			return 1;
		}
		DecimalFormat df2 = new DecimalFormat("0.00000");

		double num_one = Double.valueOf(df2.format(num1));
		double num_two = Double.valueOf(df2.format(num2));
		if (num_one == 0 && num_two == 0) {
			return 0;
		} else if (num_one == 0) {
			return -1;
		} else if (num_two == 0) {
			return 1;
		}
		double sss = (num_one) / num_two;
		return sss;

	}

	// 整型，千分位分隔
	public static String trans2Int(Object a) {
		if (null == a) {
			return "--";
		}
		DecimalFormat df3 = new DecimalFormat("#,###");
		return df3.format(Double.valueOf(String.valueOf(a)));
	}

	// 浮点，千分位分隔，两位小数
	public static String trans2Float(double num) {
		DecimalFormat df = new DecimalFormat("#,##0.00");
		if (num == 0) {
			return "0";
		}
		return df.format(num);
	}

	/**
	 * 根据类型将值转换成不同数字格式
	 * 
	 * @author zhaoxiang 统一格式
	 * @param value
	 * @param type
	 *            0 : 整型 2：浮点 2：百分比
	 * @return
	 */
	public static String tranValueByTypeForIE(Object value, String type) {
		if (value == null || StringUtils.isBlank(value.toString()) || StringUtils.equals("--", value.toString())) {
			return "--";
		}

		int colType = 0; // 数据类型 0 : 整型 ， 1：百分比 ，2:浮点型
		// 如果是百分比要保留4位小数
		if ("0".equals(type)) {
			colType = 0;
		} else if ("1".equals(type)) {
			colType = 1;
		} else if ("2".equals(type)) {
			colType = 2;
		} else if (String.valueOf(value).contains(".")) {
			colType = 2;
		}

		String result = "";
		switch (colType) {
		case 0: // 整形，千分位分隔
			result = trans2Int(value);
			break;
		case 1: // 保留四位小数,百分比用
			result = transTo4Point(Double.parseDouble(value.toString()));
			break;
		case 2: // 浮点型,整数部门千分位分隔，小数部分两位
			result = trans2Float(Double.parseDouble(value.toString()));
			break;
		case 3: // 百分比，两位小数
			result = comRate(value.toString());
			break;
		}
		return result;
	}

	public boolean isSelected(String value, String value2) {
		if (StringUtils.equalsIgnoreCase(value, value2)) {
			return true;
		}
		return false;
	}

}
